package org.dizem.freepl0.gui.listener;

import org.dizem.freepl0.gui.controller.FPController;
import org.dizem.freepl0.gui.panel.EditPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: DIZEM
 * Date: 11-2-2
 * Time: 下午3:41
 */
public class FPListener implements ActionListener {
	private FPController controller;

	public FPListener(FPController controller) {
		this.controller = controller;
	}

	public void actionPerformed(ActionEvent e) {
		String id = ((Component)(e.getSource())).getName();
		if(id.equals("toolbar_run")) {
			if(controller.compile()) {
				controller.run();
			}

		} else if(id.equals("toolbar_new")) {
			String filename = JOptionPane.showInputDialog(controller.getTabPanel(), "Input File Name");
			if(filename != null) {
				controller.addNewTab(filename + ".pl0");
			}

		} else if(id.equals("toolbar_save")) {
			controller.saveFile();
			JOptionPane.showMessageDialog(controller.getTabPanel(), "Saved successfully.");

		} else if(id.equals("toolbar_copy")) {
			controller.getTabPanel().getCurrentTab().copy();

		} else if(id.equals("toolbar_paste")) {
			controller.getTabPanel().getCurrentTab().paste();

		} else if(id.equals("toolbar_cut")) {
			controller.getTabPanel().getCurrentTab().cut();

		}  else if(id.equals("toolbar_about")) {
			controller.showAbout();

		} else if(id.equals("toolbar_open")) {
			controller.openFile();

		} else if(id.equals("toolbar_modify")) {
			EditPanel editor = controller.getTabPanel().getCurrentTab();
			editor.setText(editor.getText().replaceAll("\t", "    "));
		}


	}
}
